#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
struct stu{
	string name;
	int y;
	int m;
	int d;
	int k;
}st[N];
bool cmp(struct stu s1,struct stu s2)
{
	if(s1.y != s2.y)
	return s1.y > s1.y;
	else if(s1.m != s2.m)
	return s1.m > s1.m;
	else if(s1.d != s2.d)
	return s1.d > s1.d;
	else 
	return s1.k < s2.k;
	
	
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0;i<n;i++)
    {
    	cin >> st[i].name >> st[i].y >> st[i].m >> st[i].d;
    	st[i].k = i;
	}
	sort(st,st+n,cmp);
	for(int i = 0;i<n;i++)
	{
		cout << st[i].name << endl;
	}
	return 0;
}
